package MemberLogService

import (
	"context"
	"gdshop-back-end-go/common/request/TaskDataReq"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
)

type MemberLogType uint8

const (
	// 会员登录
	MemberLogin MemberLogType = iota + 1
	// 会员退出
	MemberLogout
	// 会员修改密码
	MemberChangePassword
	// 会员修改资料
	MemberChangeInfo
	// 会员修改地址
	MemberChangeAddress
	// 会员修改支付密码
	MemberChangePayPassword
	// 会员修改支付方式
	MemberChangePayType
	// 会员余额变动
	MemberChangeBalance = 30
	// 会员积分变动
	MemberChangePoint
	// 会员优惠券变动
	MemberChangeCoupon = 40
)

func AddLog(ctx context.Context, logType MemberLogType, data *TaskDataReq.MemberLog) {
	err := g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		AddLogTx(tx, logType, data)
		return nil
	})
	if err != nil {
		return
	}
}

func AddLogTx(tx *gdb.TX, logType MemberLogType, data *TaskDataReq.MemberLog) {
	data.Type = int(logType)
	data.CreateTime = gtime.Now().Unix()
	_, err := tx.Model("member_log").Data(data).Save()
	if err != nil {
		return
	}
}
